﻿using System.Diagnostics;
using System;
using ISRecord.View.Properties;
using System.IO;

namespace ISRecord.View.Services
{
    internal static class Logger
    {
        const string LOGFILENAME = "IORT Log File.log";
        private static string LogFilePath
        {
            get
            {
                return Settings.Default.LogFilePath;
            }
        }
        private static string LogFileName
        {
            get
            {
                if (!string.IsNullOrEmpty(LogFilePath))
                {
                    return Path.Combine(LogFilePath, LOGFILENAME);
                }
                return LOGFILENAME;
            }
        }

        internal static void Error(string message, string source, string methodName)
        {
            WriteEntry(message, "Error", source, methodName);
        }
        internal static void Error(Exception ex, string source, string methodName)
        {
            WriteEntry(ex.Message, "Error", source, methodName);
        }
        internal static void Warning(string message, string source, string methodName)
        {
            WriteEntry(message, "Warning", source, methodName);
        }
        internal static void Info(string message, string source, string methodName)
        {
            WriteEntry(message, "Info", source, methodName);
        }
        private static void WriteEntry(string message, string messageType, string source, string methodName )
        {
            using (Stream fileStream = new FileStream(LogFileName, FileMode.Append))
            {
                using (TextWriterTraceListener listener = new TextWriterTraceListener(fileStream, "appListener"))
                {
                    Trace.Listeners.Add(listener);
                    
                    Trace.WriteLine(
                       string.Format("{0}, {1}, Source: {2}, Method Name: {3}, {4}",
                       DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                       messageType,
                       source,
                       methodName,
                       message));
                    Trace.Flush();
                    Trace.Close();
                }
            }
        }
       
        
    }
}
